<?php

namespace app\rongcui\controller;

use app\department\model\UserModel;
use cmf\controller\AdminBaseController;

class UserController extends AdminBaseController
{
    public function select()
    {
        $admin_id = cmf_get_current_admin_id();
        $where = [];
        if ($admin_id != 1) {
            $department = get_user_group($admin_id);
            $departments = getParent('department',$department[0],['id','lvl','is_company']);
            $parent = [];
            foreach ($departments as $department) {
                if($department['is_company']) {
                    $parent = $department;
                    break;
                }
            }

            $where['dep.id'] = ['in', getGroups($parent)];
        }
        $id = $this->request->param('id',0,'intval');
        $type = $this->request->param('type',0,'intval');
        $userModel = new UserModel();
        if($type) {
            $userModel->join('__ROLE_USER__ role_user', 'role_user.user_id = user.id')
            ->join('__ROLE_ITEM__ role_item', 'role_user.role_id = role_item.role_id and config_id = 1 and item_id = '.$type);
        }

        $where['user_status'] = ['neq',0];
        $lists = $userModel
            ->alias('user')
            ->join('__DEPARTMENT_USER__ dep_user', 'user.id = dep_user.user_id')
            ->join('__DEPARTMENT__ dep', 'dep_user.dep_id = dep.id')
            ->where([
                'user.id' => ['neq',$admin_id]
            ])
            ->where($where)
            ->field([
                'user.*',
                'dep.name as dep_name,dep.id as dep_id'
            ])->select();

        $this->assign('lists', $lists);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function check()
    {
        $admin_id = cmf_get_current_admin_id();
        $where = [];
        if ($admin_id != 1) {
            $department = get_user_group($admin_id);
            $departments = getParent('department',$department[0],['id','lvl','is_company']);
            $parent = [];
            foreach ($departments as $department) {
                if($department['is_company']) {
                    $parent = $department;
                    break;
                }
            }

            $where['dep.id'] = ['in', getGroups($parent)];
        }
        $ids = $this->request->param('id','');
        $type = $this->request->param('type',0,'intval');
        $ids = explode(',',$ids);
        $userModel = new UserModel();
        if($type) {
            $userModel->join('__ROLE_USER__ role_user', 'role_user.user_id = user.id')
                ->join('__ROLE_ITEM__ role_item', 'role_user.role_id = role_item.role_id and config_id = 1 and item_id = '.$type);
        }

        $where['user_status'] = ['neq',0];
        $lists = $userModel
            ->alias('user')
            ->join('__DEPARTMENT_USER__ dep_user', 'user.id = dep_user.user_id')
            ->join('__DEPARTMENT__ dep', 'dep_user.dep_id = dep.id')
            ->where([
                'user.id' => ['neq',$admin_id]
            ])
            ->where($where)
            ->field([
                'user.*',
                'dep.name as dep_name,dep.id as dep_id'
            ])->select();

        $this->assign('lists', $lists);
        $this->assign('ids', $ids);
        return $this->fetch();
    }
}